package com.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;

import com.bean.User;
import com.util.DatabaseManger;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Administrator on 2018/1/30.
 */

public class UserService {
  public static  boolean addUser(Context context, User user) {
      DatabaseManger databaseManger=DatabaseManger.getInstance(context);
      String sql="insert into user (name,phone,address,password) values(?,?,?,?)";
      String[] params={user.getName(),user.getPhone(),user.getAddress(),user.getPassword()};
      try {
          databaseManger.insertDataBySql(sql, params);
      } catch (Exception e) {
          return false;
      }finally {
          databaseManger.close();
      }
      return  true;
  }
    public static  User checkUser(Context context,String phone,String password ){
        DatabaseManger databaseManger=DatabaseManger.getInstance(context);
         User user=null;
        String sql="select * from user where phone=? and password=?";
        String[]params={phone,password};
        try {
            Cursor cursor=databaseManger.queryData2Cursor(sql,params);
            if(cursor!=null&&cursor.getCount()>0){

                if(cursor.moveToNext()) {
                    Log.d("test",cursor.getString(0));
                    user=new User(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            databaseManger.close();
        }
        return user;
    }
    public static boolean isLogin(Context context){
        SharedPreferences pref = context.getSharedPreferences("session",MODE_PRIVATE);
        boolean isLogin = pref.getBoolean("isLogin",false);//第二个参数为默认值
        return isLogin;
    }
    public static User getSession(Context context){
        SharedPreferences pref = context.getSharedPreferences("session",MODE_PRIVATE);
        String name = pref.getString("name","");
        String phone=pref.getString("phone","");
        String address=pref.getString("address","");
        String password=pref.getString("password","");
       User user=new User(phone,name,address,password);
        return user;
    }
    public static void setSession(User user,Context context){
        SharedPreferences pref = context.getSharedPreferences("session",MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("name",user.getName());
        editor.putString("phone",user.getPhone());
        editor.putString("address",user.getAddress());
        editor.putString("password",user.getPassword());
        editor.putBoolean("isLogin",true);
        editor.apply();
    }
    public static void removeSession(Context context){
        SharedPreferences pref = context.getSharedPreferences("session",MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        editor.apply();
    }

}
